*&---------------------------------------------------------------------*
*& 包含               YBC_SWS_DELETE_REQUEST_CLS
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& 包含               YBC_SWS_REPORT_REQ_LOG_CLS
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
*       CLASS lcl_events DEFINITION
*----------------------------------------------------------------------*
* SALV Event Handler Definition                                        *
*----------------------------------------------------------------------*
CLASS GCL_EVENTS_HANDLER DEFINITION.
  PUBLIC SECTION.
    METHODS:
      ON_LINK_CLICK FOR EVENT LINK_CLICK  "Hotspot Handler
        OF CL_SALV_EVENTS_TABLE
        IMPORTING ROW COLUMN,

      ON_USER_COMMAND FOR EVENT ADDED_FUNCTION OF CL_SALV_EVENTS
        IMPORTING E_SALV_FUNCTION.
ENDCLASS.                    "lcl_events DEFINITION
*
*----------------------------------------------------------------------*
*       CLASS lcl_events IMPLEMENTATION
*----------------------------------------------------------------------*
*  SAL Event Handler Methods                                           *
*----------------------------------------------------------------------*
CLASS GCL_EVENTS_HANDLER IMPLEMENTATION.
  METHOD ON_LINK_CLICK.
    DATA:
      LV_JSON    TYPE STRING,
      LS_REQJSON TYPE YBC_SWS_REQJSON,
      LS_REQUEST TYPE YBC_SWS_REQUEST.

    CLEAR LS_REQUEST.
    READ TABLE GT_RESULT INTO LS_REQUEST INDEX ROW.
    IF SY-SUBRC = 0.
      SELECT SINGLE
        *
        INTO LS_REQJSON
        FROM YBC_SWS_REQJSON
       WHERE REQUESTID = LS_REQUEST-REQUESTID.

      IF LS_REQJSON-ITEMHJSON IS INITIAL.
        LS_REQJSON-ITEMHJSON = '""'.
      ENDIF.

      IF LS_REQJSON-ITEMRJSON IS INITIAL.
        LS_REQJSON-ITEMRJSON = '""'.
      ENDIF.

      IF LS_REQJSON-ATTACHJSON IS INITIAL.
        LS_REQJSON-ATTACHJSON = '""'.
      ENDIF.

      IF LS_REQJSON-ACTIONJSON IS INITIAL.
        LS_REQJSON-ACTIONJSON = '""'.
      ENDIF.

      LV_JSON = '{ "HEADJSON": ' && LS_REQJSON-HEADJSON &&
                ', "ITEMHJSON": ' && LS_REQJSON-ITEMHJSON &&
                ', "ITEMRJSON": ' && LS_REQJSON-ITEMRJSON &&
                ', "ATTACHJSON": ' && LS_REQJSON-ATTACHJSON &&
                ', "ACTIONJSON": ' && LS_REQJSON-ACTIONJSON &&
                ' }'.

      CALL TRANSFORMATION SJSON2HTML SOURCE XML LV_JSON
                               RESULT XML DATA(LV_HTML).

      TRY .
          CL_ABAP_BROWSER=>SHOW_HTML( HTML_STRING =
            CL_ABAP_CODEPAGE=>CONVERT_FROM( LV_HTML ) ).

        CATCH CX_XSLT_RUNTIME_ERROR INTO DATA(REF_ERROR).
          MESSAGE TEXT-T01 TYPE 'E'.
      ENDTRY.

    ENDIF. "row in ALV obtained
  ENDMETHOD.                    "on_link_click

  METHOD ON_USER_COMMAND.
    DATA:
      LR_COLUMNS    TYPE REF TO CL_SALV_COLUMNS_TABLE.

    DATA:
      LV_TEXT       TYPE STRING,
      LS_MSG        TYPE BAPIRET2,
      LV_ANSWER     TYPE CHAR1,
      LV_ROW_COUNT  TYPE I,
      LS_RESULT     TYPE YBC_SWS_REQUEST,
      LT_SELECT_D   TYPE STANDARD TABLE OF YBC_SWS_REQUEST,
      LT_SELECT_ROW TYPE SALV_T_ROW,
      LS_SELECT_ROW TYPE I,
      LR_SELECTS    TYPE REF TO CL_SALV_SELECTIONS.

    CASE E_SALV_FUNCTION.
      WHEN '&ZDELETE'.

        LR_SELECTS = GR_TABLE->GET_SELECTIONS( ).
        LT_SELECT_ROW = LR_SELECTS->GET_SELECTED_ROWS( ).
        LV_ROW_COUNT = LINES( LT_SELECT_ROW ).

        IF LV_ROW_COUNT <> 0.

          CALL FUNCTION 'POPUP_TO_CONFIRM'
            EXPORTING
              TITLEBAR       = TEXT-T02
              TEXT_QUESTION  = TEXT-T02
            IMPORTING
              ANSWER         = LV_ANSWER
            EXCEPTIONS
              TEXT_NOT_FOUND = 1
              OTHERS         = 2.
          IF SY-SUBRC <> 0.
            MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.

          IF LV_ANSWER = '1'.
            LOOP AT LT_SELECT_ROW INTO LS_SELECT_ROW.
              CLEAR LS_RESULT.
              READ TABLE GT_RESULT INTO LS_RESULT INDEX LS_SELECT_ROW.
              IF SY-SUBRC = 0.
                DELETE FROM YBC_SWS_APPROVER WHERE REQUESTID = LS_RESULT-REQUESTID.
                DELETE FROM YBC_SWS_REQUEST WHERE REQUESTID = LS_RESULT-REQUESTID.
                DELETE FROM YBC_SWS_REQJSON WHERE REQUESTID = LS_RESULT-REQUESTID.
                DELETE FROM YBC_SWS_REQHEAD WHERE REQUESTID = LS_RESULT-REQUESTID.
                DELETE FROM YBC_SWS_ATTACH WHERE REQUESTID = LS_RESULT-REQUESTID.

                COMMIT WORK AND WAIT.
                IF SY-SUBRC <> 0.
                  ROLLBACK WORK.
                ENDIF.
              ENDIF.
            ENDLOOP.
          ENDIF.

          PERFORM FORM_GET_DATA.

          LR_COLUMNS = GR_TABLE->GET_COLUMNS( ).
          LR_COLUMNS->SET_OPTIMIZE( 'X' ).
          GR_TABLE->REFRESH( ).
        ENDIF.

      WHEN OTHERS.
    ENDCASE.
  ENDMETHOD.
ENDCLASS.                    "lcl_events IMPLEMENTATION

DATA: GR_EVENTS_HANDLER TYPE REF TO GCL_EVENTS_HANDLER.
